package com.yq.cns.adapter

import android.content.Context
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.yq.cns.R
import com.yq.cns.entity.BottomNavigatorEntity
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView

/**
 * @author: user
 * @date: 2022/11/17
 */
class HomeBottomNavigatorAdapter(
    val map: Map<Int, BottomNavigatorEntity>,
    var itemClickListener: ItemClickListener? = null
) :
    CommonNavigatorAdapter() {

    override fun getCount() = map.size

    var selectIndex = 0
    override fun getTitleView(context: Context, index: Int): IPagerTitleView {
        val commonPagerTitleView = CommonPagerTitleView(context)
        val contentView =
            LayoutInflater.from(context).inflate(R.layout.layout_indicator_tab_bottom, null)
        commonPagerTitleView.setContentView(contentView)
        val ivIcon = contentView.findViewById<ImageView>(R.id.iv_tab_indicator)
        val tvText = contentView.findViewById<TextView>(R.id.tv_tab_indicator)
        val bottomNavigatorEntity = map[index]
        bottomNavigatorEntity?.let {
            tvText.text = it.name
            ivIcon.setImageResource(it.normalIcon)
        }

        commonPagerTitleView.onPagerTitleChangeListener =
            object : CommonPagerTitleView.OnPagerTitleChangeListener {
                override fun onSelected(index: Int, totalCount: Int) {
                    selectIndex = index
                    bottomNavigatorEntity?.let {
                        ivIcon.setImageResource(it.selectedIcon)
                        tvText.setTextColor(
                            ContextCompat.getColor(
                                context,
                                R.color.tabbar_selected_title_color
                            )
                        )
                    }
                }

                override fun onDeselected(index: Int, totalCount: Int) {
                    bottomNavigatorEntity?.let {
                        ivIcon.setImageResource(it.normalIcon)
                        tvText.setTextColor(
                            ContextCompat.getColor(
                                context,
                                R.color.tabbar_unselected_title_color
                            )
                        )
                    }
                }

                override fun onLeave(
                    index: Int,
                    totalCount: Int,
                    leavePercent: Float,
                    leftToRight: Boolean
                ) {

                }

                override fun onEnter(
                    index: Int,
                    totalCount: Int,
                    enterPercent: Float,
                    leftToRight: Boolean
                ) {

                }

            }

        commonPagerTitleView.setOnClickListener {
            if (selectIndex == index) {
                itemClickListener?.onTabClickRepeate(index)
            } else {
                itemClickListener?.onTabClick(index)
            }
        }
        return commonPagerTitleView

    }

    override fun getIndicator(context: Context?) = null


    interface ItemClickListener {
        fun onTabClick(index: Int)
        fun onTabClickRepeate(index: Int)
    }
}